home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / J4FTUT03.ZIP / temp / techtut / tut3 / ptech3.txt next >
Encoding:
Text File  |  1997-09-10  |  10.2 KB  |  310 lines

  1.  
  2.  ========================================================================  
  3.                    Just4Fun Productions presents:
  4.    
  5.                       Part 3 of: the Techtutor
  6.                        (Topics never covered)  
  7.  
  8.  
  9.                              HighScores
  10.  ======================================================================== 
  11.  
  12. Introduction
  13. ============-
  14. Welcome to part 3 of the Techtutor, this one is mainly for SuperFX users, 
  15. because it explains how to read a string using the keyboard. It does however 
  16. contains some information for beginners interested in putting a great 
  17. highscore table in their game...
  18. For an example of a very stylish high-score, read TECH03.PAS
  19.  
  20. ----------------------------------------------------------------
  21. For contacting Just4Fun or me (PeeBee) use the following ways:
  22.  
  23. Internet : http://people.zeelandnet.nl/rpb
  24. Email    : just4fun@zeelandnet.nl
  25.  
  26. SnailMail: P.Bestebroer
  27.      Anthony Edenlaan 28
  28.      4463 JC  Goes (Zld.)
  29.       Holland
  30.  
  31. ICQ      : 2309110 (probably fastest way to contact me)
  32. ----------------------------------------------------------------
  33.  
  34. Need more input...
  35. ==================-
  36.  
  37. First of all we'll need to get the name of the player, and the best way 
  38. would be to let him/her type it. One problem occurs when using the engine, 
  39. because we don't have the normal keyboard interupt...
  40. We could ofcourse switch back to the old one, but it can be done different.
  41. The Engine contains an array wich has the "names" of all the keys, and
  42. we are going to use that array... For the example, take a look at the 
  43. GETINPUT procedure (tech03.pas) This procedure will get the input of the 
  44. player, and return it. Note that this procedure could still use some work, 
  45. like checking on ESCAPE to exit the input, and it has a few bugs in it. But
  46. the most important thing is shown, the checking on the correct key-pressed.
  47.  
  48. HighScore typecasting
  49. =====================-
  50.  
  51. Before we code anything, we'll have to define a HIGHSCORE type like this:
  52.  
  53.      TYPE  TScore = record
  54.             name   : string[8];
  55.             score  : longint;
  56.             level  : word;
  57.             date   : Longint;
  58.            end;
  59.   
  60.      VAR   HiScore  : array[1..15] of Tscore;
  61.  
  62.  
  63. The example will record the following things about the new high-score:
  64.  * Name of player, the most important item
  65.  * Score of player, very nice to know...
  66.  * Level reached, the level the player reached.
  67.  * Date of game, this is to show when the game was played...
  68. Ofcourse not all these are needed, but just use what you want in you'r
  69. game, and simply erase the rest. Now that we have the type, we made an array 
  70. of 1..15, so we can record 15 players..again this can be changed for you'r 
  71. own desires.
  72.  
  73. Who makes the list...
  74. =====================-
  75.  
  76. The next thing we should do is checking if the player has a new highscore.
  77. To do this, we'll have to walk thru the existing list, and see if the
  78. score is larger then the lowest...if it is we need to know if it's higher
  79. then the next, and the next, and the next, etc... 
  80.  
  81. {=-=-= Example =-=-=-=}
  82. FUNCTION SeeHIScore(Nscore:longint;Nlevel:word):boolean;
  83. VAR i,j  : byte;
  84.     done : boolean;
  85.     dow,hs      : word;
  86.     tempD        : DateTime;
  87.  
  88. BEGIN
  89.  SeeHiscore:=false;
  90.  if Nscore<HiScore[15].score then exit;{ if score is lower than last 
  91. one,exit}
  92.  i:=15;
  93.  done:=false;
  94.  repeat
  95.     if Nscore>HiScore[i].score then dec(i) else Done:=true;
  96.  until (i=0) or (done);
  97.     inc(i);
  98.     SeeHiScore:=true;
  99.     for j:=14 downto i do HiScore[j+1]:=HiScore[j];
  100.     with HiScore[i] do begin             { add new highscore }
  101.          Name:=GetName;                  { get the name }
  102.  
  103.          score := Nscore;                { add score }
  104.          level := NLevel;                { add level }
  105.          GetDate(TempD.year,TempD.month,TempD.day,dow);
  106.          GetTime(TempD.hour,TempD.min,TempD.sec,hs);
  107.          PackTime(TempD,date);           { add date  }
  108.     end;
  109. END;
  110. {=-=-= End of Example =-=-=-=}
  111.  
  112. This procedure will not only look if we got an highscore, it will also
  113. add the new score to the list, and call the GETINPUT procedure for the name.
  114.  
  115. Show me, I wanna see it
  116. =======================-
  117.  
  118. So now we got a high-score table...but we still can't see it. Showing the 
  119. table is the most simple thing in the SuperFX Engine, just walk thru the 
  120. table, and write the text on screen! You could, ofcourse, first draw a 
  121. background or something and then put the text "over" it. For the example 
  122. look at SHOWHISCORE. It will display the complete table (NOTE: this 
  123. procedure is different from the one found in the example, this one can be 
  124. used without the SuperFX engine).
  125.  
  126.  
  127. {=-=-=-= Example =-=-=-=}
  128. PROCEDURE ShowHIScore;  { for normal text-screens   untested!!! }
  129. VAR i    : byte;
  130.     zin  : string;
  131.     Tdate: DateTime;
  132.     ScoreOFS : byte;
  133. BEGIN
  134.  textcolor(7); textbackground(0); clrscr; 
  135.  ScoreOfs:=2;    
  136.  
  137.  REPEAT
  138.    gotoxy(1,1); write('NR');       
  139.    gotoxy(4,1,); write('Name'); 
  140.    gotoxy(14,1); write('Score');
  141.    gotoxy(20,1); write('Level');
  142.    gotoxy(26,1); write('Time');
  143.    gotoxy(36,1); write('Date');
  144.  
  145.    for i:=1 to 15 do begin
  146.      with HiScore[i] do begin
  147.           {
  148.              Show NAME and rank
  149.           }
  150.           str(i,zin);
  151.           while length(zin)<2 do zin:=' '+zin;
  152.           zin:=zin+'. '+NAME;
  153.     gotoxy(1,scoreofs+i); write(zin);
  154.           {
  155.             Show the Score
  156.           }
  157.           str(score,zin);
  158.           while length(zin)<9 do zin:='.'+zin;
  159.           gotoxy(4,scoreofs+i); write(zin);
  160.           {
  161.             Show the level
  162.           }
  163.           str(level,zin);
  164.           gotoxy(20,scoreofs+i); write(zin);
  165.           {
  166.             Get the time of the played game
  167.           }
  168.           UnpackTime(Date,TDate);
  169.           {
  170.              Show the hour played
  171.           }
  172.           str(Tdate.hour,zin);
  173.           while length(zin)<2 do zin:=' '+zin;
  174.           zin:=zin+':';
  175.           gotoxy(26,scoreofs+i); write(zin);
  176.           {
  177.             Show minutes
  178.           }
  179.           str(Tdate.Min,zin);
  180.           while length(zin)<2 do zin:='0'+zin;
  181.           gotoxy(29,scoreofs+i); write(zin);
  182.           {
  183.             Show month name
  184.           }
  185.           zin:=MNames[Tdate.Month];
  186.           zin:=zin+'-';
  187.           gotoxy(36,scoreofs+i); write(zin);
  188.           {
  189.             Show the day
  190.           }
  191.           str(Tdate.day,zin);
  192.           while length(zin)<2 do zin:=' '+zin;
  193.           gotoxy(40,scoreofs+i); write(zin);
  194.           {
  195.             Show the Year
  196.           }
  197.           str(Tdate.year,zin);
  198.           gotoxy(43,scoreofs+i); write(zin);
  199.      end;
  200.    end;
  201.  
  202.  repeat until port[$60]<>156;
  203. END;
  204.  
  205. {=-=-=-= End of Example =-=-=-=}
  206.  
  207. Loading the score
  208. =================-
  209.  
  210. A highscore should ofcourse be saved to disk, so it can be loaded later
  211. when the game is played again... so that's what we are going to do.
  212. First of all we should be able to load the existing High-score table.
  213. You could start loading the file, but what if the file does not exist?
  214. so at the start of you'r game make sure you "fill" the high-score with
  215. some fake values, but remember that the player has to be able to easily
  216. get higher scores then the fake ones! Now we have to check if the file 
  217. exists, if it does we can safely start loading. Note that you don't go and 
  218. read in a loop from 1 to 15 (or 10), because maybe some one tinkered with 
  219. the scores and there might only be 4 highscores in the file. So keep track 
  220. of that..for an example look at the LOADSCORES procedure.
  221. Note: You could add a nice thing here, because if you find somethings wrong 
  222. with the highscore file, you could create a new blank table, and put in very 
  223. hi scores the list...just a thought.
  224.  
  225. {=-=-=-= Example =-=-=-=}
  226. PROCEDURE LoadScores;
  227. VAR f : file;
  228.     i : byte;
  229.     dow,hs      : word;
  230.     tempD       : DateTime;
  231. BEGIN
  232.  assign(f,'hiscore.his');       { name of the hiscore file }
  233.  {$I-}
  234.    reset(f,1);
  235.  {$I+}
  236.   if ioresult<>0 then begin     { No hiscore file found, so create one! }
  237.      rewrite(f,1);
  238.      Hiscore[01].score:=1500;  Hiscore[02].score:=1400;
  239.      Hiscore[03].score:=1300;  Hiscore[04].score:=1200;
  240.      Hiscore[05].score:=1100;  Hiscore[06].score:=1000;
  241.      Hiscore[07].score:=0900;  Hiscore[08].score:=0800;
  242.      Hiscore[09].score:=0700;  Hiscore[10].score:=0600;
  243.      Hiscore[11].score:=0500;  Hiscore[12].score:=0400;
  244.      Hiscore[13].score:=0300;  Hiscore[14].score:=0200;
  245.      Hiscore[15].score:=0100;
  246.      for i:=1 to 15 do begin
  247.          with HiScore[i] do begin
  248.               Name:='ME!';
  249.               Level:=$101;
  250.               GetDate(TempD.year,TempD.month,TempD.day,dow);
  251.               GetTime(TempD.hour,TempD.min,TempD.sec,hs);
  252.               PackTime(TempD,date);
  253.          end;
  254.          blockwrite(f,hiscore[i],sizeof(TScore));
  255.      end;
  256.      close(f);
  257.      exit; { done the loading, so exit }
  258.   end;
  259.  i:=1;
  260.  while (not eof(f)) and (i<16) do begin
  261.        blockread(f,hiscore[i],sizeof(TSCORE));
  262.        inc(i);
  263.  end;
  264.  close(f);
  265. END;
  266. {=-=-= End of Example =-=-=}
  267.  
  268.  
  269. Saving the score
  270. ================-
  271.  
  272. After the player get's a new high-score, it's best to save it at once.
  273. because if the game crashes the player might loose it's score, and be
  274. pissed off at the game, and the programmer (you!). The saving is the easiest 
  275. of all procedures:
  276.  * Open a NEW file
  277.  * Loop thru the scores, saving them one by one...
  278. I know that you should do some error checking, because if the game is
  279. run from CD, you won't be able to save! 
  280.  
  281. {=-=-= Example =-=-=}
  282. PROCEDURE SaveScores;
  283. VAR f : file;
  284.     i : byte;
  285.     dow,hs      : word;
  286.     tempD        : DateTime;
  287. BEGIN
  288.  assign(f,'hiscore.his');
  289.  rewrite(f,1);
  290.  for i:=1 to 15 do blockwrite(f,hiscore[i],sizeof(TSCORE));
  291.  close(f);
  292. END;
  293. {=-=-= End of Example =-=-=-=}
  294.  
  295.  
  296. That's all folks
  297. ================-
  298.  
  299. Well that was a brief explanation about high-scores, and how to
  300. implement them. I know that most people won't even look at this because
  301. they think it's to simple for them, well they had to learn it somewhere
  302. and might not even be doing it that correctly, who knows? But for the 
  303. beginning game-programmers this could be a usefull text, and for the SuperFX 
  304. Engine users it handy to know who to read the keyboard keys...
  305.  
  306. Next Tech topic: Collisions
  307.  
  308.  
  309. PeeBee - September 10th '97
  310.